<template>
  <div class="wrap">
    <el-row>
      <el-col :span="24" class="header-wrap">
        <el-form :inline="true" :model="formInline" :rules="rules"  ref="formInline">
          <el-form-item label="公司名" prop="name">
            <el-input v-model="formInline.channelName" prefix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="date"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search('formInline')">搜索</el-button>
            <el-button type="primary" @click="reset('formInline')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!--表格-->
      <el-col :span="24">
        <el-table :data="getChannelList" :border="true" @selection-change="handleSelectionChange" style="width: 100%;min-width: 1200px;">
          <el-table-column label="公司名称" align="center" min-width="140px;">
            <template slot-scope="scope">
              <span>{{ scope.row.channelName }}</span>
            </template>
          </el-table-column>

          <el-table-column label="链接" align="center">user
            <template slot-scope="scope">
              <span>{{ scope.row.channelUrl }}</span>
            </template>
          </el-table-column>
          <el-table-column label="注册量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.registerCount }}</span>
            </template>
          </el-table-column>
          <el-table-column label="借款量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.loanCount }}</span>
            </template>
          </el-table-column>
          <el-table-column label="通过量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.passCount }}</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="top"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 15, 20, 30]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total='total'>
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script type="text/ecmascript-6">
  import {post} from '@/utils/http'
  import {formatDateTime} from '@/utils/utils'
  export default {
    data() {
      let validateName = (rule, value, callback) => {
        if (value === '') {
          callback();
        } else {
          callback();
        }
      };
      let validatePhone = (rule, value, callback) => {
        if (value === '') {
          callback();
        } else {
          callback()
        }
      };
      return {
        arr: [],
        date: '',
        date1: '',
        date2: '',
        getChannelList: [],
        formInline: {
          channelName: '',
          channelUrl: '',
          registerCount: '',
          loanCount: '',
          passCount:''
        },
        rules: {
          name: [
            { validator: validateName, trigger: 'blur' }
          ],
          phone: [
            { validator: validatePhone, trigger: 'blur' }
          ]
        },
        status:'',
        landOrderList: [],
        currentPage: 1,
        total: 0,
        size: 10,
      }
    },
    methods: {
      dateFormat(row,column) {
        let tableTime = row[column.property];
        if (tableTime == undefined) {
          return "";
        }
        return this.moment(tableTime).format("YYYY-MM-DD HH:mm:ss");
      },
      handleSelectionChange(val) {
       this.arr = val;
      },
      getTime() {
        let starttime = '';
        let endtime = '';
        let time = this.date;
        if(!time[0] == '') {
          starttime = this.moment(time[0]).utc().format('YYYY-MM-DD[T]HH:mm:ss[Z]');
        }
        if(!time[1] == '') {
          endtime = this.moment(time[1]).utc().format('YYYY-MM-DD[T]HH:mm:ss[Z]');
        }
        if(starttime == "Invalid date"){
          starttime="";
        };
        if(endtime == "Invalid date"){
          endtime="";
        };
        this.date1 = starttime
        this.date2 = endtime
      },
      search(formInline) {
        this.getTime();
        this.$refs[formInline].validate((vaild) => {
          if(vaild) {
            let para = {
              pageIndex: this.currentPage,
              pageSize: this.size
            }

            if(!this.formInline.channelName.replace(/\s/g, "") == '') {
              para.channelName = this.formInline.channelName.replace(/\s/g, "")
            }
            if(!this.date1 == '') {
              para.startTime = this.date1
            }
            if(!this.date2 == '') {
              para.endTime = this.date2
            }
            post('/api/manager/v1/channel/getChannelList',para)
              .then((response) => {
              console.log(response)
                this.getChannelList = response.data;
                this.total = response.data.totalCount;
                this.size = response.data.pageSize;
              })
          }
        })
      },
      checkAll() {


      },
      reset(formInline) {
        this.$refs[formInline].resetFields();
      },
      getList() {
        post('/api/manager/v1/channel/getChannelList',{pageIndex: this.currentPage,pageSize: this.size})
          .then((response) => {
            this.getChannelList = response.data;
            this.total = response.data.totalCount;
            this.size = response.data.pageSize;
          })
      },
      handleSizeChange(val) {
        this.size = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.currentPage =val;
        this.getList();
      },

    },
    mounted(){
      this.getList();
    },
  }
</script>

<style lang='scss' scoped>
  .wrap {
    /*padding: 20px;*/
    .header-wrap{
      margin-bottom: 60px;
      background-color: #66BFBF;
      border-radius: 6px;
    }
    .el-form-item {
      margin-top: 22px;
      margin-left: 22px;
    }
    .top {
      margin-top: 10px;
      float: right;
    }
  }
</style>
